package com.youhome.business.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageInfo;
import com.youhome.common.exception.CustomException;
import com.youhome.common.response.ResultPage;
import com.youhome.common.response.Result;
import com.youhome.common.response.ResultCode;
import com.youhome.service.model.BCompany;
import com.youhome.service.model.BTemplateHtmlSelected;
import com.youhome.service.model.BUser;
import com.youhome.service.model.Principal;
import com.youhome.service.service.BCompanyService;
import com.youhome.service.service.BTemplateHtmlSelectedService;
import com.youhome.service.service.BUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

/**
 * Created by chen365 on 2017/12/25.
 */
@RestController
@Api(tags = "二级主页")
public class SecondaryHomePageController {

    @Autowired
    private BUserService bUserService;

    @Autowired
    private BCompanyService bCompanyService;

    @Autowired
    private BTemplateHtmlSelectedService bTemplateHtmlSelectedService;


    /**
     * 权限暂时开放（待 options 403 错误解决再关闭）
     *
     * @param data
     * @return
     */
    @ApiOperation(value = "提交模版与模块数据", notes = "提交模版与模块数据", response = Result.class, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String", name = "data", value = "数据", required = true, paramType = "query")
    })
    @PostMapping(value = "/template/html/data/save")
    public Object data(String data, @ApiIgnore @AuthenticationPrincipal Principal principal) throws CustomException {
        bTemplateHtmlSelectedService.save(principal,data);
        return new Result(ResultCode.SUCCESS);
    }


    @ApiOperation(value = "获取模版与模块数据", notes = "获取模版与模块数据", response = Result.class, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String", name = "uuid", value = "公司uuid", required = true, paramType = "query")
    })
    @GetMapping(value = "/template/html/data/details")
    public Object templateData(String uuid) throws CustomException {
        return new Result(ResultCode.SUCCESS, bTemplateHtmlSelectedService.selectIssueByUUID(uuid));
    }


    @ApiOperation(value = "列表模版与模块数据", notes = "列表模版与模块数据", response = Result.class, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "int", name = "pageNumber", value = "页码", paramType = "query"),
            @ApiImplicitParam(dataType = "int", name = "pageSize", value = "每页数", paramType = "query"),
            @ApiImplicitParam(dataType = "int", name = "status", value = "状态", paramType = "query")
    })
    @GetMapping(value = "/template/html/data/list")
    public Object dateList(Integer pageNumber, Integer pageSize, Integer status,@ApiIgnore @AuthenticationPrincipal Principal principal) throws CustomException {
        return new ResultPage(ResultCode.SUCCESS, bTemplateHtmlSelectedService.selectByBean(principal,status, pageNumber, pageSize));
    }

    @ApiOperation(value = "公司管理员预览二级主页", notes = "公司管理员预览二级主页", response = Result.class, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "Long", name = "id", value = "id", required = true, paramType = "query")
    })
    @GetMapping(value = "/template/html/data/preview")
    public Object preview(Long id, @ApiIgnore @AuthenticationPrincipal Principal principal) throws CustomException {
        return new Result(ResultCode.SUCCESS, bTemplateHtmlSelectedService.previewSecondaryHomePage(principal,id));
    }

    @ApiOperation(value = "提交模版审核", notes = "提交模版审核", response = Result.class, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "Long", name = "id", value = "ID", required = true, paramType = "query")
    })
    @PutMapping(value = "/template/html/submit/audit")
    public Object submitAudit(Long id, @ApiIgnore @AuthenticationPrincipal Principal principal) throws CustomException {
        bTemplateHtmlSelectedService.submitAudit(principal,id);
        return new Result(ResultCode.SUCCESS);
    }

    @ApiOperation(value = "发布/取消发布", notes = "发布/取消发布", response = Result.class, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "Long", name = "id", value = "id", required = true, paramType = "query")
    })
    @PutMapping(value = "/template/html/data/issue")
    public Object issue(Long id, @ApiIgnore @AuthenticationPrincipal Principal principal) throws CustomException {
        bTemplateHtmlSelectedService.publishUnPublish(principal,id);
        return new Result(ResultCode.SUCCESS);
    }

}
